DECLARE @Id1 AS INT
DECLARE @Id2 AS INT
DECLARE @Cnt1 AS INT
DECLARE @Cnt2 AS INT

SELECT @Cnt1 = COUNT(*) FROM C_MATCH_RULE
WHERE LIST_ID = 5
PRINT @Cnt1

IF(@Cnt1 > 0)
BEGIN
	SET @Id1 = 0
	SET @Id2 = 1
	WHILE (@Id1 <= @Cnt1)
	BEGIN
		SELECT MD.LIST_COL_ID, FIELD_NAME, MATCH_OPERATOR INTO #tmpRule FROM C_MATCH_RULE MR
		INNER JOIN C_MATCH_RULE_DETAIL MD ON MR.MATCH_RULE_ID = MD.MATCH_RULE_ID
		INNER JOIN X_MASTER_LIST_COLUMNS LC ON LC.LIST_COL_ID = MD.LIST_COL_ID
		WHERE MD.MATCH_RULE_ID = @Id1
		
		WHILE(@Id2 < 2)
		BEGIN
			SELECT * INTO #tmpDim FROM T_CONTACT_INPUT
			WHERE contact_id = @Id2
			
			--INSERT INTO T_CONTACT_MATCH_RESULT
			--(INPUT_ID, MASTER_ID, MATCH_RULE_ID)
			SELECT INPUT_ID, TD.contact_id, @Id1 FROM #tmpDim TD
			INNER JOIN M_CONTACTS MC ON TD.contact_id = MC.contact_id
			WHERE MC.first_name = TD.first_Name
			
			
			
			DROP TABLE #tmpDim
			SET @Id2 = @Id2 + 1
		END
		
		DROP TABLE #tmpRule
		SET @Id1 = @Id1 + 1
	END
END